#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import sys
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from env import Env


def __sendqqmail__(username, password, mailfrom, mailto, subject, content):
    # 邮箱的服务地址
    gserver = 'smtp.163.com'
    gport = 25
    try:
        msg = MIMEMultipart()
        # 在Python 3中，默认字符串就是unicode，不需要再进行编码转换
        msg.attach(MIMEText(content, 'html', 'utf-8'))
        msg['from'] = mailfrom
        msg['to'] = mailto
        msg['Reply-To'] = mailfrom
        msg['Subject'] = subject
        smtp = smtplib.SMTP(gserver, gport)
        smtp.set_debuglevel(0)
        smtp.ehlo()
        smtp.login(username, password)
        smtp.sendmail(mailfrom, mailto, msg.as_string())
        smtp.close()
    except Exception as err:
        print(f"Send mail failed. Error: {err}")


def send_mail(to, subject, content):
    mailinfo = Env.get_mail_info()
    if mailinfo is None or len(mailinfo.get("username")) == 0:
        print("邮件配置信息为空")
        return
    if len(to) == 0:
        print("邮件接收人为空")
        return
    to = ",".join(to)
    __sendqqmail__(mailinfo["username"], mailinfo["password"], mailinfo["mailfrom"], to, subject, content)


# def main():
#     to = sys.argv[1]
#     subject = sys.argv[2]
#     content = sys.argv[3]
#     # 定义QQ邮箱的账号和密码，你需要修改成你自己的账号和密码
#     sendqqmail('13077995907@163.com', 'CYZIKPQXTABPQDFC', '13077995907@163.com', to, subject, content)


if __name__ == "__main__":
    pass

#####脚本使用说明######
# 1. 首先定义好脚本中的邮箱账号和密码
# 2. 脚本执行命令为：python mail.py 目标邮箱 "邮件主题" "邮件内容"
